package com.example.forum.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

/**
 * 统一返回结果
 * @param <T>
 */
@Data
public class AppResult<T> {
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private Integer code;

    /**
     * 错误信息
     */
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String errorMsg;

    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;

    public AppResult(Integer code, String errorMsg, T data) {
        this.code = code;
        this.errorMsg = errorMsg;
        this.data = data;
    }

    public AppResult(Integer code, String errorMsg) {
        this.code = code;
        this.errorMsg = errorMsg;
    }
    public static <T> AppResult<T> success() {
        return new AppResult<>(ResultCode.SUCCESS.getCode(),ResultCode.SUCCESS.getMessage());
    }
    public static <T> AppResult<T> success(String errorMsg) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), errorMsg);
    }

    public static <T> AppResult<T> success(T data) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);
    }
    public static <T> AppResult<T> success(String errorMsg,T data) {
        return new AppResult<>(ResultCode.SUCCESS.getCode(), errorMsg,data);
    }


    public static <T> AppResult<T> fail() {
        return new AppResult<>(ResultCode.FAILED.getCode(),ResultCode.FAILED.getMessage());
    }
    public static <T> AppResult<T> fail(String errorMsg) {
        return new AppResult<>(ResultCode.FAILED.getCode(),errorMsg);
    }

    public static <T> AppResult<T> fail(String errorMsg,Integer code) {
        return new AppResult<>(code,errorMsg);
    }

    public static <T> AppResult<T> fail(ResultCode resultCode) {
        return new AppResult<>(resultCode.getCode(),resultCode.getMessage());
    }
}
